package com.songtech.base.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.songtech.base.listener.SysSessionContext;

public class BaseInterceptor implements HandlerInterceptor{

	private Logger logger = LoggerFactory.getLogger(BaseInterceptor.class);
	
	//无需拦截的url
	private static String[] exclusiveUrl = {"/"};

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {
		String _token = request.getParameter("_token");
		if(StringUtils.isNotEmpty(_token)){
			//String token = tokenParam.substring(6,tokenParam.length());
			HttpSession session = SysSessionContext.getSession(_token);
			if(session != null ){
				Object sessionObj =  session.getAttribute(_token);
				if(sessionObj != null){
					return true;
				}
			}
		}
		logger.info(" current operation is not permitted you must login ");
		// response.sendRedirect("login");
		return false;
	}
	
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object obj, Exception ex)
			throws Exception {
	}
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object obj, ModelAndView view)
			throws Exception {
	}
}
